package com.gxa.weixing.pc.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.ShopGoodsConsume;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.core.util.VerifyParamUtil;
import com.gxa.weixing.pc.mapper.GoodsDetailMapper;
import com.gxa.weixing.pc.mapper.ShopGoodsConsumeMapper;
import com.gxa.weixing.pc.service.ShopGoodsConsumeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2022/01/06  16:59:37
 */
@Service("shopGoodsConsume")
@Transactional(rollbackFor = Throwable.class)
public class ShopGoodsConsumeServiceImpl extends BaseServiceImpl<ShopGoodsConsume> implements ShopGoodsConsumeService {

    @Resource
    private ShopGoodsConsumeMapper shopGoodsConsumeMapper;

    @Resource
    private GoodsDetailMapper goodsDetailMapper;

    @Resource
    private HttpServletResponse httpServletResponse;

    /**
     * 根据特定条件获取门店自耗商品列表的方法
     *
     * @param map 条件
     *            name: 商品名称 模糊
     *            shopName:所属门店名称
     *            state: 审核状态 (0 审核中/1 审核通过/ 2审核失败)
     *            page:页码
     *            size:每页数据量
     * @return 返回查询结果
     */
    @Override
    public PageInfo<Map<String, Object>> getList(Map<String, Object> map) throws BusinessException, ParseException {
        // 获取分页信息
        Integer page = VerifyParamUtil.pageDefaultOneVerify(map.get("page"));
        Integer size = VerifyParamUtil.sizeInfiniteVerify(map.get("size"));
        // 开启分页
        PageMethod.startPage(page, size);

        // 获取其他字段
        // 商品名称
        String name = VerifyParamUtil.stringVerifyParam(map.get("name"));
        // 门店名称
        String shopName = VerifyParamUtil.stringVerifyParam(map.get("shopName"));
        // 审核状态
        Integer state = VerifyParamUtil.integerVerifyParam(map.get("state"));
        // 返回查询到的集合
        List<Map<String, Object>> list = shopGoodsConsumeMapper.getList(name, shopName, state);
        // 对时间格式进行转换
        List<Map<String, Object>> lists = VerifyParamUtil.formatDateToList(list, "applyTime", "auditTime");

        System.out.println(lists);
        System.out.println("test------------------------------");
        // 返回带有分页的集合
        return PageInfo.of(lists);
    }

    /**
     * 自耗申请审核方法
     *
     * @param shopGoodsConsume 自耗申请对象
     * @return 返回是否成功
     */
    @Override
    public void audit(ShopGoodsConsume shopGoodsConsume) throws BusinessException {
        // 参数进行非空校验
        VerifyParamUtil.checkParams(shopGoodsConsume, shopGoodsConsume.getState(), shopGoodsConsume.getAuditorId()
                , shopGoodsConsume.getId(), shopGoodsConsume.getAuditRemarks(), shopGoodsConsume.getGoodsId(), shopGoodsConsume.getConsumeNumber());

        // 对状态进行校验 只能为1或者2
        if (shopGoodsConsume.getState() < 1 || shopGoodsConsume.getState() > 2) {
            throw new BusinessException(ExceptionCode.FORMAT_COMVERSION_ERROR);
        }

        // 如果审核为通过 则商品的门店自耗字段需要增加消耗
        if (shopGoodsConsume.getState() == 1) {
            // 进行添加消耗操作
            Integer integer = goodsDetailMapper.updateShopConsume(shopGoodsConsume.getGoodsId(), shopGoodsConsume.getConsumeNumber());
            // 如果返回值不正确 则抛出异常进行回滚
            if (integer <= 0) {
                throw new BusinessException(ExceptionCode.AUDIT_ERROR);
            }
        }
        // 进行审核操作
        Integer audit = shopGoodsConsumeMapper.audit(shopGoodsConsume.getId(), shopGoodsConsume.getState()
                , shopGoodsConsume.getAuditRemarks(), new Date(), shopGoodsConsume.getAuditorId());
        // 如果返回值不正确 则抛出异常进行回滚
        if (audit <= 0) {
            throw new BusinessException(ExceptionCode.AUDIT_ERROR);
        }
    }

    /**
     * 门店自耗申请方法
     *
     * @param shopGoodsConsume 门店自耗对象
     */
    @Override
    public void add(ShopGoodsConsume shopGoodsConsume) throws BusinessException {
        // 对参数进行非空校验
        VerifyParamUtil.checkParams(shopGoodsConsume, shopGoodsConsume.getGoodsId(), shopGoodsConsume.getUserId(), shopGoodsConsume.getConsumeNumber());
        // 指定参数默认值
        shopGoodsConsume.setState(0);
        shopGoodsConsume.setApplyTime(new Date());

        //执行添加操作
        ShopGoodsConsume shopGoodsConsumeAdd = insert(shopGoodsConsume);

        // 进行返回值校验
        if (shopGoodsConsumeAdd.getId() == null) {
            throw new BusinessException(ExceptionCode.ADD_ERROR);
        }

    }

}
